2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex5_slaveMultipleSlave.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 //******************************************************************************
34 // MSP430FR57xx Demo - USCI_B0 I2C 4 Hardware I2C slaves
35 //
36 // Description: This demo connects two MSP430's via the I2C bus.
37 // This code configures the MSP430 USCI to be addressed as 4 independent I2C
38 // slaves. Each slave has its owm interrupt flag and data variable to store
39 // incoming data.
40 //
41 // Tested on MSP430FR5739, MSP430FR5969
42 // /|\ /|\
43 // 10k 10k
44 // slave | | master
45 // ----------------- | | -----------------
46 // -|XIN P1.6/UCB0SDA|<-|----+->|P1.6/UCB0SDA XIN|-
47 // | | | | |
48 // -|XOUT | | | XOUT|-
49 // | P1.7/UCB0SCL|<-+------>|P1.7/UCB0SCL |
50 // | | | |
51 // | | | |
52 //
53 //******************************************************************************
54 uint8_t RXData0=0;
55 uint8_t RXData1=0;
56 uint8_t RXData2=0;
57 uint8_t RXData3=0;
58 
59 void main(void)
60 {
61  WDT_A_hold(WDT_A_BASE);
62 
63  // Configure Pins for I2C
64  //Set P1.6 and P1.7 as Secondary Module Function Input.
65  /*
66 
67  * Select Port 1
68  * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
69  */
70  GPIO_setAsPeripheralModuleFunctionInputPin(
71  GPIO_PORT_P1,
72  GPIO_PIN6 + GPIO_PIN7,
73  GPIO_SECONDARY_MODULE_FUNCTION
74  );
75 
76  // eUSCI configuration
77  //SLAVE0 own address is 0x0A+ enable
78  EUSCI_B_I2C_initSlaveParam initSlave0Param = {0};
79  initSlave0Param.slaveAddress = 0x0A;
80  initSlave0Param.slaveAddressOffset = EUSCI_B_I2C_OWN_ADDRESS_OFFSET0;
81  initSlave0Param.slaveOwnAddressEnable = EUSCI_B_I2C_OWN_ADDRESS_ENABLE;
82  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &initSlave0Param);
83 
84  //SLAVE1 own address is 0x0B+ enable
85  EUSCI_B_I2C_initSlaveParam initSlave1Param = {0};
86  initSlave1Param.slaveAddress = 0x0B;
87  initSlave1Param.slaveAddressOffset = EUSCI_B_I2C_OWN_ADDRESS_OFFSET1;
88  initSlave1Param.slaveOwnAddressEnable = EUSCI_B_I2C_OWN_ADDRESS_ENABLE;
89  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &initSlave1Param);
90 
91  //SLAVE2 own address is 0x0C+ enable
92  EUSCI_B_I2C_initSlaveParam initSlave2Param = {0};
93  initSlave2Param.slaveAddress = 0x0C;
94  initSlave2Param.slaveAddressOffset = EUSCI_B_I2C_OWN_ADDRESS_OFFSET2;
95  initSlave2Param.slaveOwnAddressEnable = EUSCI_B_I2C_OWN_ADDRESS_ENABLE;
96  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &initSlave2Param);
97 
98  //SLAVE3 own address is 0x0D+ enable
99  EUSCI_B_I2C_initSlaveParam initSlave3Param = {0};
100  initSlave3Param.slaveAddress = 0x0D;
101  initSlave3Param.slaveAddressOffset = EUSCI_B_I2C_OWN_ADDRESS_OFFSET3;
102  initSlave3Param.slaveOwnAddressEnable = EUSCI_B_I2C_OWN_ADDRESS_ENABLE;
103  EUSCI_B_I2C_initSlave(EUSCI_B0_BASE, &initSlave3Param);
104 
105  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
106 
107  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
108  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
109  EUSCI_B_I2C_RECEIVE_INTERRUPT1 +
110  EUSCI_B_I2C_RECEIVE_INTERRUPT2 +
111  EUSCI_B_I2C_RECEIVE_INTERRUPT3
112  );
113 
114  //receive interrupt enable
115  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
116  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
117  EUSCI_B_I2C_RECEIVE_INTERRUPT1 +
118  EUSCI_B_I2C_RECEIVE_INTERRUPT2 +
119  EUSCI_B_I2C_RECEIVE_INTERRUPT3
120  );
121 
122 
123 
124  __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
125  __no_operation();
126 }
127 
128 
129 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
130 #pragma vector=USCI_B0_VECTOR
131 __interrupt
132 #elif defined(__GNUC__)
133 __attribute__((interrupt(USCI_B0_VECTOR)))
134 #endif
135 void USCIB0_ISR(void)
136 {
137  switch(__even_in_range(UCB0IV,0x1E))
138  {
139  case 0x00: break; // Vector 0: No interrupts break;
140  case 0x02: break; // Vector 2: ALIFG break;
141  case 0x04: break; // Vector 4: NACKIFG break;
142  case 0x06: break; // Vector 6: STTIFG break;
143  case 0x08: break; // Vector 8: STPIFG break;
144  case 0x0a: // SLAVE3
145  RXData3 = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
146  break; // Vector 10: RXIFG3 break;
147  case 0x0c: break; // Vector 14: TXIFG3 break;
148  case 0x0e: // SLAVE2
149  RXData2 = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
150  break; // Vector 16: RXIFG2 break;
151  case 0x10: break; // Vector 18: TXIFG2 break;
152  case 0x12: // SLAVE1
153  RXData1 = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
154  break; // Vector 20: RXIFG1 break;
155  case 0x14: break; // Vector 22: TXIFG1 break;
156  case 0x16: // SLAVE0
157  RXData0 = EUSCI_B_I2C_slaveGetData(EUSCI_B0_BASE);
158  // Get RX data
159  break; // Vector 24: RXIFG0 break;
160  case 0x18: break; // Vector 26: TXIFG0 break;
161  case 0x1a: break; // Vector 28: BCNTIFG break;
162  case 0x1c: break; // Vector 30: clock low timeout break;
163  case 0x1e: break; // Vector 32: 9th bit break;
164  default: break;
165  }
166 
167 }
168 
169 
__no_operation()